<?php

namespace App;

class CheckEnvironment
{
    private $adopt = true;

    public function getAdopt()
    {
        return $this->adopt;
    }

    /**
     * 系统环境检测
     * @return array 系统环境数据
     */
    public function checkEnv()
    {
        $items = [
            'os'     => [
                'title'   => '操作系统',
                'limit'   => '不限制',
                'current' => PHP_OS,
                'icon'    => 'fa-check text-success',
            ],
            'php'    => [
                'title'   => 'PHP版本',
                'limit'   => '5.5+',
                'current' => PHP_VERSION,
                'icon'    => 'fa-check text-success',
            ],
            'upload' => [
                'title'   => '附件上传',
                'limit'   => '不限制',
                'current' => ini_get('file_uploads') ? ini_get('upload_max_filesize') : '未知',
                'icon'    => 'fa-check text-success',
            ],
            'gd'     => [
                'title'   => 'GD库',
                'limit'   => '2.0+',
                'current' => '未知',
                'icon'    => 'fa-check text-success',
            ],
            'disk'   => [
                'title'   => '磁盘空间',
                'limit'   => '100M+',
                'current' => '未知',
                'icon'    => 'fa-check text-success',
            ],
        ];

        //PHP环境检测
        if ($items['php']['current'] < 5.5) {
            $items['php']['icon'] = 'fa-remove text-danger';
            $this->adopt          = false;
        }

        //GD库检测
        $tmp = function_exists('gd_info') ? gd_info() : [];
        if (!$tmp['GD Version']) {
            $items['gd']['current'] = '未安装';
            $items['gd']['icon']    = 'fa-remove text-danger';
            $this->adopt            = false;
        } else {
            $items['gd']['current'] = $tmp['GD Version'];
        }
        unset($tmp);

        //磁盘空间检测
        if (function_exists('disk_free_space')) {
            $disk_size                = floor(disk_free_space('./') / (1024 * 1024)) . 'M';
            $items['disk']['current'] = $disk_size . 'MB';
            if ($disk_size < 100) {
                $items['disk']['icon'] = 'fa-remove text-danger';
                $this->adopt           = false;
            }
        }

        return $items;
    }

    /**
     * 函数检测
     * @return array 检测数据
     */
    public function checkFuncAndExt()
    {
        $items = [
            '0' => [
                'type'    => 'ext',
                'name'    => 'pdo',
                'title'   => '支持',
                'current' => extension_loaded('pdo'),
                'icon'    => 'fa-check text-success',
            ],
            '1' => [
                'type'    => 'ext',
                'name'    => 'pdo_mysql',
                'title'   => '支持',
                'current' => extension_loaded('pdo_mysql'),
                'icon'    => 'fa-check text-success',
            ],
            '2' => [
                'type'  => 'func',
                'name'  => 'file_get_contents',
                'title' => '支持',
                'icon'  => 'fa-check text-success',
            ],
            '3' => [
                'type'  => 'func',
                'name'  => 'mb_strlen',
                'title' => '支持',
                'icon'  => 'fa-check text-success',
            ],
        ];
        foreach ($items as &$val) {
            switch ($val['type']) {
                case 'ext':
                    if (!$val['current']) {
                        $val['title'] = '不支持';
                        $val['icon']  = 'fa-remove text-danger';
                        $this->adopt  = false;
                    }
                    break;
                case 'func':
                    if (!function_exists($val['name'])) {
                        $val['title'] = '不支持';
                        $val['icon']  = 'fa-remove text-danger';
                        $this->adopt  = false;
                    }
                    break;
            }
        }

        return $items;
    }

    /**
     * 目录，文件读写检测
     * @return array 检测数据
     */
    public function checkDirFile()
    {
        $items = [
            '0' => [
                'type'  => 'dir',
                'path'  => storage_path(),
                'title' => '可写',
                'icon'  => 'fa-check text-success',
            ],

            '1' => [
                'type'  => 'dir',
                'path'  => base_path('bootstrap/cache'),
                'title' => '可写',
                'icon'  => 'fa-check text-success',
            ],
            '2' => [
                'type'  => 'file',
                'path'  => base_path() . '/.env',
                'title' => '可写',
                'icon'  => 'fa-check text-success',
            ],
        ];

        foreach ($items as &$val) {
            $path = $val['path'];
            if ('dir' === $val['type']) {
                if (!is_writable($path)) {
                    if (is_dir($path)) {
                        $val['title'] = '不可写';
                        $val['icon']  = 'fa-remove text-danger';
                        $this->adopt  = false;

                    } else {
                        $val['title'] = '不存在';
                        $val['icon']  = 'fa-remove text-danger';
                        $this->adopt  = false;

                    }
                }
            } else {
                if (file_exists($path)) {
                    if (!is_writable($path)) {
                        $val['title'] = '不可写';
                        $val['icon']  = 'fa-remove text-danger';
                        $this->adopt  = false;

                    }
                } else {
                    if (!is_writable(dirname($path))) {
                        $val['title'] = '不存在';
                        $val['icon']  = 'fa-remove text-danger';
                        $this->adopt  = false;

                    }
                }
            }
        }
        return $items;
    }

}
